package com.darwin.darwinUI.controls
{
	import com.darwin.darwinUI.containers.Box;
	import com.darwin.darwinUI.core.UIComponent;
	import com.darwin.darwinUI.events.UIEvent;
	import com.darwin.darwinUI.uiconst.UIAlign;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.utils.getTimer;
	
	/**
	 *	 列表组件
	 * @author Roland
	 * 
	 */
	public class List extends Box
	{
		
		//========================================================
		//  								公共变量
		//========================================================
		
		
		//========================================================
		//  								私有变量
		//========================================================
		
		private var _itemClass:Class;//创建子项的类 
		private var _itemWidth:Number;//子项宽度 
		private var _itemHeight:Number;//子项高度 
		private var _itemHDistance:Number;//子项间水平间距
		private var _itemVDistance:Number;//子项间垂直间距
		
		private var _cItemHDistance:Number;//当前列表实际的间距
		private var _cItemVDistance:Number;//当前列表实际的间距 
		private var _cHCount:int = 0;
		private var _cVCount:int = 0;
		
		private var _page1:Box;//页容器1 
		private var _page2:Box;//页容器2 
		
		private var _page1Index:int = 0;//_page1对应的页索引项
		private var _page2Index:int = 1;//_page2对应的页索引项
		private var _cPage:Box;//当前页的页容器
		
		private var _sizeChange:Boolean = false;
		private var _refreshList:Boolean = false;
		private var _selectedIndex:int = -1;

		private var _source:Array;//列表数据
		
		//是否处于滚动状态的
		private var _rolling:Boolean = false;
		private var _preEnterFrameTime:int = 0;
		//滚动速度
		private var _rollSpeedX:Number = 0;
		private var _rollSpeedY:Number = 0;
		
		//目标位置
		private var _targetX:Number = 0;
		private var _targetY:Number = 0;
		//========================================================
		//  								构造函数
		//========================================================
		
		/**
		 *	 实例化一个列表组件
		 * @param itemClass 列表子项的类(必须继承UIComponent并实现IListItem接口)
		 * @param itemWidth 子项宽度
		 * @param itemHeight 子项高度
		 * @param itemHDistance 子项水平间距
		 * @param itemVDistance 子项垂直间距
		 * @param direction 子项排列方式 横向排列：UIAlign.LAYOUT_HORIZONTAL 纵向排列： UIAlign.LAYOUT_VERTICAL
		 * 
		 */
		public function List(itemClass:Class=null,itemWidth:Number=10,itemHeight:Number=10,
							 itemHDistance:Number = 0,itemVDistance:Number = 0,
							 direction:String = UIAlign.LAYOUT_HORIZONTAL)
		{
			super();
			this._itemClass = itemClass;
			this._itemWidth = itemWidth;
			this._itemHeight = itemHeight;
			this._itemVDistance = itemVDistance;
			this._itemHDistance = itemHDistance;
			
			this.itemDistance = (direction == UIAlign.LAYOUT_HORIZONTAL) ? itemHDistance : itemVDistance;
			
			_page1 = new Box();
			_page1.align = (direction == UIAlign.LAYOUT_HORIZONTAL) ? UIAlign.LAYOUT_VERTICAL : UIAlign.LAYOUT_HORIZONTAL;
			_page1.itemDistance = (direction == UIAlign.LAYOUT_HORIZONTAL) ? itemVDistance : itemHDistance;
			_page1.autoSize = false;
			_page2 = new Box();
			_page2.align =  (direction == UIAlign.LAYOUT_HORIZONTAL) ? UIAlign.LAYOUT_VERTICAL : UIAlign.LAYOUT_HORIZONTAL;
			_page2.itemDistance = (direction == UIAlign.LAYOUT_HORIZONTAL) ? itemVDistance : itemHDistance;
			_page2.autoSize = false;
			addItem(_page1);
			addItem(_page2);
			
			super.align =  direction;
			super.layoutFunc = listLayoutFn;
			this.autoSize = false;

			_cPage = _page1;
			_sizeChange = true;
			
			if(this.stage) this.stage.invalidate();
			addEventListener(Event.ENTER_FRAME,onEnterFrame);
		}
		
		
		//========================================================
		//  								公共方法
		//========================================================
		/**
		 *	立即刷新列表数据 
		 * @param v
		 * 
		 */
		public function refreshList(v:Array):void
		{
			_refreshList = true;
			source = v;
			_refreshList = false;
		}
		
		/**
		 * 设置和取得当前被选中的列表项
		 */
		public function get selectedIndex():int
		{
			return _selectedIndex;
		}

		public function set selectedIndex(value:int):void
		{
			if(source != null && source.length > value && _selectedIndex != value)
			{
				_selectedIndex = value;
				var i:int;
				var $pageArr:Array = [_page1,_page2];
				for(var j:int = 0;j<$pageArr.length;j++)
				{
					var $page:Box = $pageArr[j];
					for(i = 0;i<$page.numItems;i++)
					{
						var $line:Box = $page.getItemAt(i) as Box;
						for (var k:int = 0; k < $line.numItems; k++) 
						{
							if(($line.getItemAt(k) as IListItem).index == value)
							{
								($line.getItemAt(k) as IListItem).selected = true;
							}else{
								if(($line.getItemAt(k) as IListItem).selected == true) ($line.getItemAt(k) as IListItem).selected = false;
							}
						}

					}
				}
				dispatchEvent(new UIEvent(UIEvent.LIST_SELECTED_INDEX_CHANGE));
			}
		}
		
		/**
		 * @return 返回当前列表对象的列表项(只能拿到实例化过的两页中列表项实例)
		 */
		public function getListItem():Array
		{
			var pageList:Array = [_page1,_page2];
			var arr:Array = [];
			var i:int;
			var j:int;
			var k:int;
			var line:Box;
			var page:Box;
			for (k = 0; k < pageList.length; k++) 
			{
				page = pageList[k];
				for(i = 0;i<page.numItems;i++)
				{
					line = page.getItemAt(i) as Box;
					for (j = 0; j < line.numItems; j++) 
					{
						arr.push(line.getItemAt(j));
					}
				}
			}
			return arr;
		}
		
		/**
		 *	滚动到某一页所在位置 
		 * @param pageIndex 页索引(从0开始)
		 * @param time 滚动过程所花费的时间(毫秒)
		 * 
		 */
		public function rollToPage(pageIndex:int,time:Number):void
		{
			if(pageIndex<0)pageIndex = 0;
			if(time <= 0) time = 1;
			switch(this.align)
			{
				case UIAlign.LAYOUT_HORIZONTAL:
				{
					rollTime(getPageX(pageIndex),0,time);
					break;
				}
				case UIAlign.LAYOUT_VERTICAL:
				{
					rollTime(0,getPageY(pageIndex),time);
					break;
				}
				default:
				{
					rollTime(getPageX(pageIndex),0,time);
					break;
				}
			}
		}
		
		/**
		 *	滚动到某一项所在位置
		 * @param itemIndex 项索引(从0开始)
		 * @param time 滚动过程所花费的时间(毫秒)
		 * 
		 */
		public function rollToItem(itemIndex:int,time:Number):void
		{
			if(itemIndex < 0)itemIndex = 0;
			if(time <= 0) time = 1;
			switch(this.align)
			{
				case UIAlign.LAYOUT_HORIZONTAL:
				{
					rollTime(getItemX(itemIndex),0,time);
					break;
				}
				case UIAlign.LAYOUT_VERTICAL:
				{
					rollTime(0,getItemY(itemIndex),time);
					break;
				}
				default:
				{
					rollTime(getItemX(itemIndex),0,time);
					break;
				}
			}
		}
		
		/**
		 * 滚动到一个位置,滚动速度固定
		 * @param x 目标x值
		 * @param y 目标y值
		 * @param speed 滚动速度,像素/毫秒
		 */
		public function rollSpeed(x:Number,y:Number,speed:Number):void
		{
			_preEnterFrameTime = getTimer();
			_targetX = x;
			_targetY = y;
			if(_targetX < this.scrollX)
			{
				_rollSpeedX = speed*-1;
			}
			else if(_targetX > this.scrollX)
			{
				_rollSpeedX = speed;
			}else{
				_rollSpeedX = 0;
			}
			
			
			if(_targetY < this.scrollY)
			{
				_rollSpeedY = speed*-1;
			}else if(_targetY > this.scrollY){
				_rollSpeedY = speed;
			}else{
				_rollSpeedY = 0;
			}
			
			_rolling = true;
		}
		
		/**
		 * 滚动到一个位置，在一定时间内滚动完成
		 * @param x 目标x值
		 * @param y 目标y值
		 * @param time 多长时间，毫秒
		 */
		public function rollTime(x:Number,y:Number,time:int):void
		{
			if(_rolling==false)
			{
				var $x:Number = Math.abs(this.scrollX - x)/time;
				var $y:Number = Math.abs(this.scrollY - y)/time;
				var $speed:Number = Math.max($x,$y);
				rollSpeed(x,y,$speed);
			}
		}
		
		
		/**
		 * 停止滚动，如果方法调用时列表滚动列表项半项上则会继续滚动直到下一项。
		 * 要列表停留在整页上可使用stopRollToPage
		 */
		public function stopRoll():void
		{
			if(_rolling == true)
			{
				var $itemIndex:int = toItemIndex(this.scrollX,this.scrollY);
				var $tX:Number = getItemX($itemIndex);
				var $tY:Number = getItemY($itemIndex);
				if(_rollSpeedX > 0)
				{
					$tX += (_itemWidth + _cItemHDistance);
				}
				if(_rollSpeedY > 0)
				{
					$tY += (_itemHeight + _cItemVDistance);
				}
				
				_targetX = $tX;
				_targetY = $tY;
			}
		}
		
		
		/**
		 * 停止滚动，如果方法调用时列表滚动半页上则会继续滚动直到下页。
		 * 想要列表停留在列表项上可使用stopRoll
		 */
		public function stopRollToPage():void
		{
			if(_rolling == true)
			{
				var $pageIndex:int = toPageIndex(this.scrollX,this.scrollY);
				var $tX:Number = 0;
				var $tY:Number = 0;
				if(this.align == UIAlign.LAYOUT_HORIZONTAL)
				{
					$tX = getPageX($pageIndex);
				}else{
					$tY = getPageY($pageIndex);
				}
				
				if(_rollSpeedX > 0)
				{
					$tX += (_itemWidth + _cItemHDistance)*_cHCount;
				}
				if(_rollSpeedY > 0)
				{
					$tY += (_itemHeight + _cItemVDistance)*_cVCount;
				}
				
				_targetX = $tX;
				_targetY = $tY;
			}
		}
		
		
		
		
		/**
		 * 设子容器的相对于容器的x值，x值增大则列表项往左滚动，x值减小则列表项往右滚动
		 * @param value
		 */
		override public function set scrollX(value:Number):void
		{
			super.scrollX = value;
			if(super.align != UIAlign.LAYOUT_HORIZONTAL) return;
			var $prePage1Index:int = _page1Index;
			var $prePage2Index:int = _page2Index;
			
			var pageWidth:Number = _cHCount * (_itemWidth + _cItemHDistance);
			var $index:int = (value/pageWidth+0.0001);//得出当前页是page1还是page2
			var isChange:Boolean = false;
			if($index % 2 == 0)
			{
				if(_page1Index != $index || _cPage != _page1)
				{//如果当前页需要改变
					_cPage = _page1;
					_page1Index = $index;
					_page2Index = $index + 1;
					isChange = true;
				}
			}else{
				if(_page2Index != $index || _cPage != _page2)
				{//如果当前页需要改变
					_cPage = _page2;
					_page1Index = $index +1;
					_page2Index = $index;
					isChange = true;
				}
			}
			if(isChange)
			{//如果当前改变则重新计算项位置
				_cPage.x = pageWidth * $index;
				setPagePosition();
				
				if(_page1Index != $prePage1Index)
				{
					setData(_page1,_page1Index);
				}
				if(_page2Index != $prePage2Index)
				{
					setData(_page2,_page2Index);
				}
			}
		}
		
		/**
		 * 设子容器的相对于容器的y值，y值增大则列表项往上滚动，y值减小则列表项往下滚动
		 * @param value
		 */
		override public function set scrollY(value:Number):void
		{
			super.scrollY = value;
			if(super.align != UIAlign.LAYOUT_VERTICAL) return;
			var $prePage1Index:int = _page1Index;
			var $prePage2Index:int = _page2Index;
			
			var pageHeight:Number = _cVCount * (_itemHeight + _cItemVDistance);
			var $index:int = (value/pageHeight+0.0001);//得出当前页是page1还是page2
			var isChange:Boolean = false;
			if($index % 2 == 0)
			{
				if(_page1Index != $index || _cPage != _page1)
				{//如果当前页需要改变
					_cPage = _page1;
					_page1Index = $index;
					_page2Index = $index + 1;
					isChange = true;
				}
			}else{
				if(_page2Index != $index || _cPage != _page2)
				{//如果当前页需要改变
					_cPage = _page2;
					_page1Index = $index +1;
					_page2Index = $index;
					isChange = true;
				}
			}
			
			if(isChange)
			{//如果当前改变则重新计算项位置
				_cPage.y = pageHeight * $index;
				setPagePosition();
				
				if(_page1Index != $prePage1Index)
				{
					setData(_page1,_page1Index);
				}
				
				if(_page2Index != $prePage2Index)
				{
					setData(_page2,_page2Index);
				}
				
			}
		}
		
		//========================================================
		//  								保护方法
		//========================================================
		/**
		 * 根据高宽创建列表项实例
		 */
		protected function createChild(page:Box,itemClass:Class,hCount:int,vCount:int):void
		{
			page.removeAllItem();
			var $lineAlign:String = (page.align == UIAlign.LAYOUT_HORIZONTAL) ? UIAlign.LAYOUT_VERTICAL : UIAlign.LAYOUT_HORIZONTAL;
			var $lineCount:int = (page.align == UIAlign.LAYOUT_HORIZONTAL) ? hCount : vCount;
			var $lineWidth:Number = ($lineAlign == UIAlign.LAYOUT_HORIZONTAL) ? page.width : _itemWidth;
			var $lineHeight:Number = ($lineAlign == UIAlign.LAYOUT_HORIZONTAL) ? _itemHeight : page.height;
			var $lineItemDistance:Number =  ($lineAlign == UIAlign.LAYOUT_HORIZONTAL) ? _itemHDistance : _itemVDistance;
			var $itemCount:int = ($lineAlign == UIAlign.LAYOUT_HORIZONTAL) ? hCount : vCount;
			for (var i:int = 0; i < $lineCount; i++) 
			{
				var $line:Box = new Box();
//				$line.rectVisible = true;
				$line.width = $lineWidth;
				$line.height = $lineHeight;
				$line.autoSize = false;
				$line.align = $lineAlign;
				$line.itemDistance = $lineItemDistance;
				for (var j:int = 0; j < $itemCount; j++) 
				{
					var $item:UIComponent = new itemClass();
					if($item is IListItem)
					{
						$item.addEventListener(MouseEvent.CLICK,itemClickHandler);
						$item.width = _itemWidth;
						$item.height = _itemHeight;
						$line.addItem($item);
					}else{
						throw new Error("错误：itemClass没有实现IListItem接口，传递给List的itemClass必须继承UIComponent并实现IListItem接口!");
					}
				}
				page.addItem($line);		
			}
		}
		
		/**
		 *项单击处理 
		 * @param e
		 * 
		 */
		protected function itemClickHandler(event:MouseEvent):void
		{
			dispatchEvent(new UIEvent(UIEvent.LISTITEM_CLICK,false,false,event.currentTarget));
		}
		
		protected function onEnterFrame(event:Event):void
		{
			if(_rolling)
			{
				var $currentTime:int = getTimer();
				var $time:int = $currentTime - _preEnterFrameTime;
				_preEnterFrameTime = $currentTime;
				this.scrollX += $time * _rollSpeedX;
				this.scrollY += $time * _rollSpeedY;
				
				
				var $arrivalX:Boolean = false;
				var $arrivalY:Boolean = false;
				if(_rollSpeedX < 0)
				{
					if(this.scrollX < _targetX) $arrivalX = true;
				}else if(_rollSpeedX > 0){
					if(this.scrollX > _targetX) $arrivalX = true;
				}else{
					$arrivalX = true;
				}
				
				if(_rollSpeedY < 0)
				{
					if(this.scrollY < _targetY) $arrivalY = true;
				}else if(_rollSpeedY > 0){
					if(this.scrollY > _targetY) $arrivalY = true;
				}else{
					$arrivalY = true;
				}
				
				
				if($arrivalX && $arrivalY)
				{
					_rolling = false;
					this.scrollX = _targetX;
					this.scrollY = _targetY;
					dispatchEvent(new UIEvent(UIEvent.LIST_ROLL_COMP));
				}
				
			}
		}
		
		override protected function refresh():void
		{
			super.refresh();
		}
		
		override protected function updateDisplayList():void
		{
			super.updateDisplayList();
			if(_sizeChange)
			{
				
				
				//add--------------------------
				if(_itemClass)
				{
					operationCount(this.width,this.height,_itemWidth,_itemHeight,_itemHDistance,_itemVDistance);
					createChild(_page1,_itemClass,_cHCount,_cVCount);
					createChild(_page2,_itemClass,_cHCount,_cVCount);
					_sizeChange = false;
					setPagePosition();
					setData(_page1,_page1Index);
					setData(_page2,_page2Index);
				}
				//------------------------------
				
			}
		}
		
		//========================================================
		//  								私有方法
		//========================================================
		/**
		 * 设置page的位置
		 */
		private function setPagePosition():void
		{//计算非当前页是当前的上一页还是下一页并算出非当前页的位置
			var $cPage:UIComponent;
			var $otherPage:UIComponent;
			var $otherPageIsNext:Boolean = true;
			if(_cPage == _page1)
			{
				$cPage = _page1;
				$otherPage = _page2;
				if(_page2Index > _page1Index) $otherPageIsNext = true;
				else $otherPageIsNext = false;
			}else{
				$cPage = _page2;
				$otherPage = _page1;
				if(_page1Index > _page2Index) $otherPageIsNext = true;
				else $otherPageIsNext = false;
			}
			
			//调整索引位置
			if(align == UIAlign.LAYOUT_HORIZONTAL)
			{
				if($otherPageIsNext)
				{
					$otherPage.x = Math.ceil($cPage.x + (_itemWidth + _cItemHDistance) * _cHCount);
				}else{
					$otherPage.x = Math.ceil($cPage.x - (_itemWidth + _cItemHDistance) * _cHCount);
				}
				$otherPage.y = $cPage.y;
			}else{
				if($otherPageIsNext)
				{
					$otherPage.y = Math.ceil($cPage.y + (_itemHeight + _cItemVDistance) * _cVCount);
				}else{
					$otherPage.y = Math.ceil($cPage.y - (_itemHeight + _cItemVDistance) * _cVCount);
				}
				$otherPage.x = $cPage.x;
			}
		}
		/**
		 *  计算每页的水平及垂直数量，和水平及垂直间距
		 * @param listWidth 列表宽度
		 * @param listHeight 列表高度
		 * @param itemWidth 子项宽度
		 * @param itemHeight 子项高度
		 * @param hDistance 横向间距
		 * @param vDistance 纵向间距
		 * 
		 */		
		private function operationCount(listWidth:Number,listHeight:Number,
										itemWidth:Number,itemHeight:Number,
										hDistance:Number,vDistance:Number):void
		{
			if(listWidth < itemWidth || listHeight < itemHeight)
			{
				_cHCount = 0;
				_cVCount = 0;
			}else{
				_cHCount = getItemCount(listWidth,itemWidth,hDistance);//水平项个数
				_cVCount = getItemCount(listHeight,itemHeight,vDistance);//垂直项个数
			}
			if(_cHCount>1)
			{
				_cItemHDistance = (listWidth - itemWidth*_cHCount)/(_cHCount-1);
			}else{
				_cItemHDistance = 0;
			}
			if(_cVCount>1)
			{
				_cItemVDistance = (listHeight - itemHeight*_cVCount)/(_cVCount-1);
			}else{
				_cItemVDistance = 0;
			}
		}
		
		/**
		 * 根据取得总长度，项长度和间距得出最大的项个数
		 */
		private function getItemCount(fullValue:Number,itemValue:Number,distanceValue:Number):int
		{
			var $count:int = fullValue/(itemValue+distanceValue);
			
			while(itemValue*($count+1) + distanceValue*$count <= fullValue)
			{
				$count++;
			}
			
			return $count;
		}
		
		/**
		 * 设置 List项的数据
		 * 
		 */
		private function setData(pageObject:Box,pageIndex:int):void
		{
			if(source == null) return;
			var i:int;
			var j:int;
			var $index:int;
			var $cIndex:int;
			var $dataValue:Object;
			var $line:Box;
			var $cListItem:IListItem;
			$index = pageIndex * (_cHCount*_cVCount);//得出page1所在的索引位置，然后将这个位置的数据依次给列表项赋值
			$cIndex = 0;
			for(i = 0;i<pageObject.numItems;i++)
			{
				$line =  (pageObject.getItemAt(i) as Box);
				for (j = 0; j < $line.numItems; j++) 
				{
					$dataValue = source[$index+$cIndex];
					$cListItem = ($line.getItemAt(j) as IListItem);
					if($dataValue != null)
					{
						if($cListItem.data != $dataValue ||  _refreshList == true)
						{
							$cListItem.data = $dataValue;
						}
						($cListItem as UIComponent).visible = true;
					}else{
						($cListItem as UIComponent).visible = false;
					}
					
					//处理选中项
					if($index+$cIndex == selectedIndex)
					{
						$cListItem.selected = true;
					}else{
						if($cListItem.selected == true) $cListItem.selected = false;
					}
					$cListItem.index = $index+$cIndex;
					$cIndex++;
				}
			}
		}
		
		
		
		/**
		 *	刷新布局
		 */
		private function listLayoutFn(itemList:Vector.<UIComponent>,align:String,itemDistance:int):void
		{
			
		}
		//========================================================
		//  								getter/setter
		//========================================================
		
		/**
		 * 数据源
		 * @param v
		 */
		public function set source(v:Array):void
		{
			_source = v;
			setData(_page1,_page1Index);
			setData(_page2,_page2Index);
			
		}
		public function get source():Array
		{
			return _source;
		}
		
		/**
		 * 当前列表停留在的页数
		 * 索引从0开始
		 */
		public function get currentPage():int
		{
			if(_cPage == _page1)
			{
				return _page1Index;
			}else{
				return _page2Index;
			}
			return 0;
		}
		
		/**
		 * 最大的x值，列表滚到最后一页时的x值
		 */
		public function get maxX():Number
		{
			if(align == UIAlign.LAYOUT_VERTICAL)
			{
				return 0;
			}else{
				if(source == null) return 0;
				if(_cVCount <= 1)
				{//如果列表只有一排，则减去一排算出位置，列表中不留空
					return (source.length - _cHCount)*(_itemWidth+_cItemHDistance);
				}
				else
				{//如果多排列表，则直接得出最后一页的位置
					return (_itemWidth+_cItemHDistance)*_cHCount*(pageLength-1);
				}
			}
			return 0;
		}
		/**
		 * 最小的x值，列表滚到第一页时的x值
		 */
		public function get minX():Number
		{
			return 0;
		}
		
		/**
		 * 最大的y值
		 */
		public function get maxY():Number
		{
			if(align == UIAlign.LAYOUT_HORIZONTAL)
			{
				return 0;
			}else{
				if(source == null) return 0;
				if(_cHCount <= 1)
				{
					return (source.length - _cVCount)*(_itemHeight+_cItemVDistance);
				}
				else
				{
					return (_itemHeight+_cItemVDistance)*_cVCount*(pageLength-1);
				}
			}
			return 0;
		}
		/**
		 * 最小的y值
		 */
		public function get minY():Number
		{
			return 0;
		}
		/**
		 * 列表项数量,即数据长度
		 */
		public function get itemLength():int
		{
			if(source == null) return 0;
			return source.length;
		}
		
		/**
		 * 列表页数量
		 */
		public function get pageLength():int
		{
			return Math.ceil(itemLength/(_cHCount*_cVCount));
		}
		
		/**
		 * 水平项的个数
		 */
		public function get hCount():int
		{
			return _cHCount;
		}
		/**
		 * 垂直项的个数
		 */
		public function get vCount():int
		{
			return _cVCount;
		}
		
		/**
		 * 获得一个列表项的x位置
		 * @param itemIndex 项索引
		 * @return 项的x位置
		 */
		public function getItemX(itemIndex:int):Number
		{
			var $page:int = itemIndex/(_cHCount*_cVCount);
			var $spare:int = itemIndex%(_cHCount*_cVCount);
			$spare = $spare%_cHCount;
			var $returnValue:Number = 0;
			if(this.align == UIAlign.LAYOUT_VERTICAL)
			{
				$returnValue = $spare*(_itemWidth + _cItemHDistance);
			}else{
				$returnValue = _cHCount*(_itemWidth + _cItemHDistance)*$page + $spare*(_itemWidth + _cItemHDistance);
			}
			return $returnValue;
		}
		
		/**
		 * 获得一个列表项的y位置
		 * @param itemIndex 项索引
		 * @return 项的y位置
		 */
		public function getItemY(itemIndex:int):Number
		{
			var $page:int = itemIndex/(_cHCount*_cVCount);
			var $spare:int = itemIndex%(_cHCount*_cVCount);
			$spare = $spare/_cHCount;
			var $returnValue:Number = 0;
			if(this.align == UIAlign.LAYOUT_HORIZONTAL)
			{
				$returnValue = $spare*(_itemHeight + _cItemVDistance);
			}else{
				$returnValue = _cVCount*(_itemHeight + _cItemVDistance)*$page + $spare*(_itemHeight + _cItemVDistance);
			}
			return $returnValue;
		}
		
		/**
		 * 获得一个列表页的x位置
		 * @param pageIndex 页索引
		 * @return 页的x位置
		 */
		public function getPageX(pageIndex:int):Number
		{
			return _cHCount*(_itemWidth + _cItemHDistance)*pageIndex;
		}
		
		/**
		 * 获得一个列表页的y位置
		 * @param pageIndex 页索引
		 * @return 页的y位置
		 */
		public function getPageY(pageIndex:int):Number
		{
			return _cVCount*(_itemHeight + _cItemVDistance)*pageIndex;
		}
		
		/**
		 * 一个坐标对应的项位置
		 */
		public function toItemIndex(x:Number,y:Number):int
		{
			var $pageX:int = x/(_cHCount*(_itemWidth + _cItemHDistance));//得到页数
			var $pageY:int = y/(_cVCount*(_itemHeight + _cItemVDistance));//得到页数
			var $page:int = Math.max($pageX,$pageY);//$pageX $pageY应该有一个不大于0，实际$page取大于0的值
			var $spareX:int = x%(_cHCount*(_itemWidth + _cItemHDistance));//得到页X余数
			var $spareY:int = y%(_cVCount*(_itemHeight + _cItemVDistance));//得到页Y余数
			
			var $x:int = $spareX/(_itemWidth+_cItemHDistance);
			var $y:int = $spareY/(_itemHeight+_cItemVDistance);
			return $page*(_cHCount * _cVCount) + $y*_cHCount+$x;
		}
		
		/**
		 * 一个坐标对应的页位置
		 */
		public function toPageIndex(x:Number,y:Number):int
		{
			var $pageX:int = x/(_cHCount*(_itemWidth + _cItemHDistance));//得到页数
			var $pageY:int = y/(_cVCount*(_itemHeight + _cItemVDistance));//得到页数
			return Math.max($pageX,$pageY);
		}
		

		override public function set width(value:Number):void
		{
			super.width = value;
			_page1.width = value;
			_page2.width = value;
		}
		
		override public function set height(value:Number):void
		{
			super.height = value;
			_page1.height = value;
			_page2.height = value;
		}
		
		/**
		 * 设置列表布局，变动此属性会影响子项排列方向	 
		 * @param value
		 * 
		 */
		override public function set align(value:String):void
		{
			this.itemDistance = (value == UIAlign.LAYOUT_HORIZONTAL) ? _itemHDistance : _itemVDistance;
			this.scrollX = 0;
			this.scrollY = 0;
			super.align = value;
			_sizeChange = true;
			_page1.align = (value == UIAlign.LAYOUT_HORIZONTAL) ? UIAlign.LAYOUT_VERTICAL : UIAlign.LAYOUT_HORIZONTAL;
			_page1.itemDistance = (value == UIAlign.LAYOUT_HORIZONTAL) ? _itemVDistance : _itemHDistance;
			_page2.align = (value == UIAlign.LAYOUT_HORIZONTAL) ? UIAlign.LAYOUT_VERTICAL : UIAlign.LAYOUT_HORIZONTAL;
			_page2.itemDistance = (value == UIAlign.LAYOUT_HORIZONTAL) ? _itemVDistance : _itemHDistance;
			if(this.stage) this.stage.invalidate();
		}
		
		override public function set rectVisible(vaule:Boolean):void
		{
			super.rectVisible = vaule;
			_page1.rectVisible = vaule;
			_page2.rectVisible = vaule;
		}

		public function get itemClass():Class
		{
			return _itemClass;
		}

		public function set itemClass(value:Class):void
		{
			_itemClass = value;
			if(this.stage) this.stage.invalidate();
		}

		public function get itemWidth():Number
		{
			return _itemWidth;
		}

		public function set itemWidth(value:Number):void
		{
			_itemWidth = value;
			if(this.stage) this.stage.invalidate();
		}

		public function get itemHeight():Number
		{
			return _itemHeight;
		}

		public function set itemHeight(value:Number):void
		{
			_itemHeight = value;
			if(this.stage) this.stage.invalidate();
		}


	}
}